/**
 * This project create for studying propose at BUPT University. You can change or reuse this project anywhere
 */

package edu.bupt.ooad.library.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import edu.bupt.ooad.library.Library;

/**
 * @author Hamed Abdollahpour
 */
public class PatronPanel extends JPanel {

    private JTable table;

    private Library library;

    /**
     * Create the panel.
     */
    public PatronPanel(Library library) {
        this.library = library;

        setLayout(new BorderLayout(0, 0));

        table = new JTable();
        table.setModel(new PatronTableModel(library.getPatrons()));
        add(new JScrollPane(table), BorderLayout.CENTER);

        JPanel panel = new JPanel();
        add(panel, BorderLayout.SOUTH);

        JButton addStudentButton = new JButton("Add student");
        addStudentButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addStudent();
                table.setModel(new PatronTableModel(PatronPanel.this.library.getPatrons()));
            }
        });
        panel.add(addStudentButton);

        JButton removeButton = new JButton("Remove");
        removeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                remove();
            }
        });

        JButton addTeacherButton = new JButton("Add teacher");
        addTeacherButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addTeacher();
                table.setModel(new PatronTableModel(PatronPanel.this.library.getPatrons()));
            }
        });
        panel.add(addTeacherButton);
        panel.add(removeButton);

    }

    private void addStudent() {
        AddStudentDialog dialog = new AddStudentDialog(null);
        dialog.setVisible(true);

        if (dialog.firstname != null) {
            PatronPanel.this.library.createStudent(dialog.firstname.trim(), dialog.lastname.trim(),
                    dialog.studentId);
        }
    }

    private void addTeacher() {
        AddTeacherDialog dialog = new AddTeacherDialog(null);
        dialog.setVisible(true);

        if (dialog.firstname != null) {
            PatronPanel.this.library.createTeacher(dialog.firstname.trim(), dialog.lastname.trim(),
                    dialog.department.trim());
        }
    }

    private void remove() {
        int[] rows = table.getSelectedRows();

        if (rows.length > 0) {
            int result = JOptionPane.showConfirmDialog(this, "Do you want to remove " + rows.length
                    + "patron?", "Warning", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                PatronTableModel model = (PatronTableModel)table.getModel();

                for (int i = rows.length - 1; i >= 0; i--) {
                    int row = rows[i];

                    model.removeRow(row);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Please select least of one patrons");
        }
    }
}
